#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <cstring>
#include "UdpServer.hpp"
#include "Thread.hpp"

using namespace ThreadModlue;

int sockfd=0;
std::string server_ip;
uint16_t server_port = 0;
pthread_t id;


void Recv()
{
    while (true)
    {
        char buffer[1024];
        struct sockaddr_in peer;
        socklen_t len = sizeof(peer);
        int m = recvfrom(sockfd, buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&peer, &len);
        if (m > 0)
        {
            buffer[m] = 0;
            std::cerr << buffer << std::endl; // 2
        }
    }
}

void Send()
{
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(server_port);
    server.sin_addr.s_addr = inet_addr(server_ip.c_str());

}

int main(int argc,char *argv[])
{
    if(argc!=3)
    {
        std::cerr<<"Usage: "<<argv[0]<<"server_ip server_port"<<std::endl;
        return 1;
    }
    server_ip = argv[1];
    server_port = std::stoi(argv[2]);

    //1.创建socket
    int sockfd=socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd<0)
    {
        std::cerr<<"socket error"<<std::endl;
        return 2;
    }

    //2.创建线程
    Thread recver(Recv);
    Thread sender(Send);

    recver.Start();
    sender.Start();

    recver.Join();
    sender.Join();

    //2.client端的不需要显式绑定，类似于公共电话和自己的手机号
    //填写服务器信息
    // struct sockaddr_in server;
    // memset(&server,0,sizeof(server));
    // server.sin_family=AF_INET;
    // server.sin_port=htons(server_port);
    // server.sin_addr.s_addr=inet_addr(server_ip.c_str());
    // while(true)
    // {
    //     std::string input;
    //     std::cout<<"Please Enter# ";
    //     std::getline(std::cin,input);

    //     int n=sendto(sockfd,input.c_str(),input.size(),0,(struct sockaddr*)&server,sizeof(server));
    //     (void)n;

    //     char buffer[1024];
    //     struct sockaddr_in peer;
    //     socklen_t len=sizeof(peer);
    //     int m=recvfrom(sockfd,buffer,sizeof(buffer)-1,0,(struct sockaddr*)&peer,&len);
    //     (void)m;
    // }
    return 0;
}